package services

import (
	"GOPAAS/configs/config"
	ali_message "GOPAAS/tools/ali-message"
	"encoding/json"
)

type SmsService struct {
	AccessKeyId     string
	AccessKeySecret string
	SignName        string
}

func GetSmsService() *SmsService {
	return &SmsService{
		config.Conf.AliAccessKeyId,
		config.Conf.AliAccessKeySecret,
		config.Conf.AliSmsSignName,
	}
}

// SendVerifyCode 发送验证码
func (s SmsService) SendVerifyCode(category string, phone string, code string) (err error) {
	conf := config.SmsTemp[s.GetTempName(category)]
	conf.Data["code"] = code
	json_byte, err := json.Marshal(conf.Data)
	err = ali_message.SendSms(s.AccessKeyId, s.AccessKeySecret, phone, s.SignName, conf.Code, string(json_byte))
	return
}
func (s SmsService) GetTempName(category string) string {
	switch category {
	case BIND_MOBILE_TYPE_CACHE_KEY:
		return "信息变更验证码"
	case LOGIN_MOBILE_TYPE_CACHE_KEY:
		return "登录确认验证码"
	case REGISTER_MOBILE_TYPE_CACHE_KEY:
		return "用户注册验证码"
	case PASS_MOBILE_TYPE_CACHE_KEY:
		return "修改密码验证码"
	}
	return ""
}
